/***************************************************************************
 *   events.c                                                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "events.h"

/////////////////////////////
MGPI_Container ct;
/////////////////////////////

MGPI_BOOL keyProc(MGPI_Component *cp, SDL_Event *evt)
{
	int num;
	float4 vect = MGPI_DEFAULT_VECTOR;
	Uint8 *keys = SDL_GetKeyState(&num);
	int hcamera = cp->flags[0];
	Roeskva_t *ctx = (Roeskva_t *)cp->flags[1];

	if (keys[SDLK_ESCAPE]) return MGPI_FALSE;

	if (keys[SDLK_f]) ctx->setProperty(MGPI_TOGGLE_FULL_SCREEN);

	if (keys[SDLK_SPACE]) {
		g3dGetCamVect(&vect, hcamera, MGPI_CAMERA_UP);
        g3dTranslateCamf(hcamera, 0.2f * vect[0], 0.2f * vect[1], 0.2f * vect[2]);
    }

    if (keys[SDLK_c]) {
        g3dGetCamVect(&vect, hcamera, MGPI_CAMERA_UP);
        g3dTranslateCamf(hcamera, -0.2f * vect[0], -0.2f * vect[1], -0.2f * vect[2]);
    }

    if (keys[SDLK_w]) {
        g3dGetCamVect(&vect, hcamera, MGPI_CAMERA_FORWARD);
        g3dTranslateCamf(hcamera, 0.2f * vect[0], 0.2f * vect[1], 0.2f * vect[2]);
    }

    if (keys[SDLK_s]) {
        g3dGetCamVect(&vect, hcamera, MGPI_CAMERA_FORWARD);
        g3dTranslateCamf(hcamera, -0.2f * vect[0], -0.2f * vect[1], -0.2f * vect[2]);
    }

    if (keys[SDLK_a]) {
        g3dGetCamVect(&vect, hcamera, MGPI_CAMERA_RIGHT);
        g3dTranslateCamf(hcamera, -0.2f * vect[0], -0.2f * vect[1], -0.2f * vect[2]);
    }

    if (keys[SDLK_d]) {
        g3dGetCamVect(&vect, hcamera, MGPI_CAMERA_RIGHT);
        g3dTranslateCamf(hcamera, 0.2f * vect[0], 0.2f * vect[1], 0.2f * vect[2]);
    }
	
	return MGPI_TRUE;
}

MGPI_BOOL mouseMotionProc(MGPI_Component *cp, SDL_Event *evt) {
	int hcamera = cp->flags[0];
	static int last_x = SCREEN_WIDTH / 2;
	static int last_y = SCREEN_HEIGHT / 2;
    int current_x = evt->motion.x;
    int current_y = evt->motion.y;
    int dx = last_x - current_x;
    int dy = last_y - current_y;
    float4 right = MGPI_DEFAULT_VECTOR, up = {0.0f, 1.0f, 0.0f, 1.0f};

    g3dGetCamVect(&right, hcamera, MGPI_CAMERA_RIGHT);

    g3dRotateCamf(hcamera, (float) ((dy * MGPI_PI) / 180.0f), right);
    g3dRotateCamf(hcamera, (float) ((dx * MGPI_PI) / 180.0f), up);

    last_x = current_x;
    last_y = current_y;
    return MGPI_TRUE;
}

MGPI_BOOL mouseButtonProc(MGPI_Component *cp, SDL_Event *evt) {
	int hcamera = cp->flags[0];
    switch (evt->button.button) {
        case SDL_BUTTON_WHEELUP:
            g3dZoomIn(hcamera);
            break;

        case SDL_BUTTON_WHEELDOWN:
            g3dZoomOut(hcamera);
            break;

        default: break;
    }
    return MGPI_TRUE;
}
